package pl.jtb.ctl.tabory.h2.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BackupUtils {

	public static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy_MM_dd");
	private final static String DB_FOLDER = ".h2db";
	private final static String DB_FILE = "test.h2";
	private final static String DB_FILE_EXT = ".db";
	private final static String DB_FILE_WITH_EXT = DB_FILE + DB_FILE_EXT;
	private final static String BACKUP_FOLDER = "backup";

	public static long getLastBackupInDays() throws ParseException {
		String backupHome = getBackupPath();
		File backupFile = new File(backupHome);
		if (!backupFile.exists()) {
			return 0;
		}
		if (backupFile.isDirectory()) {
			File[] files = backupFile.listFiles();
			if (files.length == 0) {
				return 0;
			}
			Map<Date, File> mapFiledByDate = new HashMap<Date, File>();
			for (File file : files) {
				String name = file.getName();
				String datePart = name.substring(name.indexOf("_") + 1, name.lastIndexOf("."));
				Date parsedDate = DATE_FORMAT.parse(datePart);
				mapFiledByDate.put(parsedDate, file);
			}
			List<Date> listDateFiles = new ArrayList<Date>(mapFiledByDate.keySet());
			Collections.sort(listDateFiles);
			Date lastDate = listDateFiles.get(listDateFiles.size() - 1);
			Calendar currentCalendar = new GregorianCalendar();
			Calendar fileCalendar = new GregorianCalendar();
			fileCalendar.setTime(lastDate);
			long difference = currentCalendar.getTimeInMillis() - fileCalendar.getTimeInMillis();
			long diffDays = difference / (24 * 60 * 60 * 1000);
			return diffDays;
		} else {
			return 0;
		}
	}

	public static void doBackup() throws IOException {
		String dbFile = getDbFilePath();
		String backupHome = getBackupPath();
		File backupFile = new File(backupHome);
		if (!backupFile.exists()) {
			backupFile.createNewFile();
		}
		StringBuilder builder = new StringBuilder(backupHome);
		builder.append(System.getProperty("file.separator"));
		builder.append(DB_FILE).append("_");
		builder.append(DATE_FORMAT.format(new Date()));
		builder.append(DB_FILE_EXT);
		FileOutputStream backupStream = new FileOutputStream(builder.toString());
		FileInputStream dbStream = new FileInputStream(new File(dbFile));
		byte[] buffer = new byte[1024];
		int length;
		while ((length = dbStream.read(buffer)) > 0) {
			backupStream.write(buffer, 0, length);
		}
		backupStream.close();
		backupStream.close();
	}

	public static String getBackupPath() {
		StringBuilder builder = new StringBuilder();
		String backupHome = builder.append(getDbPath()).append(System.getProperty("file.separator")).append(BACKUP_FOLDER).toString();
		return backupHome;
	}

	public static String getDbPath() {
		StringBuilder builder = new StringBuilder(System.getProperty("user.home"));
		String backupHome = builder.append(System.getProperty("file.separator")).append(DB_FOLDER).toString();
		return backupHome;
	}

	public static String getDbFilePath() {
		StringBuilder builder = new StringBuilder(getDbPath());
		StringBuilder filePath = builder.append(System.getProperty("file.separator")).append(DB_FILE_WITH_EXT);
		return filePath.toString();
	}

}
